package com.imedway.ihe.dtz.xds.registry;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAssociation;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryPackage;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSubmitObjectsRequest30;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AssociationType;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.lcm.SubmitObjectsRequest;
import org.openehealth.ipf.commons.ihe.xds.core.stub.ebrs30.rs.RegistryResponseType;

import com.imedway.ihe.dtz.xds.cons.SysConsts;
import com.imedway.ihe.dtz.xds.dao.DataDao;
import com.imedway.ihe.dtz.xds.dto.XdsDto;
import com.imedway.ihe.dtz.xds.entity.IheXdsFileInfo;
import com.imedway.ihe.dtz.xds.repository.Iti41Repository_backup;
import com.imedway.ihe.dtz.xds.util.DhccFileUtils;
import com.imedway.ihe.dtz.xds.util.EbxmlInfoUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Iti42Registry_backup {

	public RegistryResponseType run(SubmitObjectsRequest body) {
		RegistryResponseType registryResponseType = new RegistryResponseType();

		try {
			boolean flag = true;
			EbXMLSubmitObjectsRequest30 ebXMLbody= new EbXMLSubmitObjectsRequest30(body);

			//保存ebxml
			EbxmlInfoUtils.saveEbxmlInfo(ebXMLbody);
			
			List<EbXMLAssociation> ebXMLAssociationList=ebXMLbody.getAssociations();
			for(EbXMLAssociation ebXMLAssociation:ebXMLAssociationList) {
				AssociationType associationType = ebXMLAssociation.getAssociationType();
				switch (associationType) {
				case SUBMIT_ASSOCIATION:
					// Document AddNew 新增
					flag = this.documentSUBMIT_ASSOCIATION(ebXMLAssociation,ebXMLbody);
					break;
				case REPLACE:
					// Document Replacement 替换
					flag = this.documentREPLACE(ebXMLAssociation,ebXMLbody);
					break;
				case APPEND:
					// Document Addendum 追加、补遗
					flag = this.documentAPPEND(ebXMLAssociation,ebXMLbody);
					break;
				case TRANSFORM:
					// Document Transformation 转换
					flag = this.documentTRANSFORM(ebXMLAssociation,ebXMLbody);
					break;
				default:
					break;
				}
			}
			
			registryResponseType.setStatus("urn:oasis:names:tc:ebxml-regrep:ResponseStatusType:" + (flag ? "success" : "fail"));

		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		return registryResponseType;
	}
	
	public boolean documentSUBMIT_ASSOCIATION(EbXMLAssociation ebXMLAssociation, EbXMLSubmitObjectsRequest30 ebXMLBody) {
		boolean flag = true;

		try {
			EbXMLExtrinsicObject ebXMLExtrinsicObject = Iti41Repository_backup.getFileMineTypeByDocumentID(ebXMLAssociation.getTarget(), ebXMLBody.getExtrinsicObjects());
			EbXMLRegistryPackage ebXMLRegistryPackage = Iti41Repository_backup.getEbXMLRegistryPackageByID(ebXMLAssociation.getSource(), ebXMLBody.getRegistryPackages());

			// 存储注册信息
			XdsDto dto = new XdsDto();
			DataDao dataDao = new DataDao();
			List<IheXdsFileInfo> iheXdsInfoList = new ArrayList<IheXdsFileInfo>();
			IheXdsFileInfo iheXdsInfo = new IheXdsFileInfo();
			String uniqueId=ebXMLRegistryPackage.getExternalIdentifierValue(SysConsts.XDSSubmissionSetuniqueIdidentificationScheme);
			iheXdsInfo.setUniqueId(uniqueId);
			iheXdsInfo.setSourceId(ebXMLRegistryPackage.getExternalIdentifierValue(SysConsts.XDSSubmissionSetsourceIdidentificationScheme));
			iheXdsInfo.setPatientId(ebXMLRegistryPackage.getExternalIdentifierValue(SysConsts.XDSSubmissionSetpatientIdidentificationScheme));			
			iheXdsInfo.setId(UUID.randomUUID().toString());
			iheXdsInfo.setCreateTime(new Timestamp(new java.util.Date().getTime()));
			iheXdsInfo.setUpdateTime(new Timestamp(new java.util.Date().getTime()));
			iheXdsInfo.setStatus("1");
//			iheXdsInfo.setFilePath(SysConsts.fileRepositoryPath+uniqueId+"."+DhccFileUtils.getFileType(ebXMLExtrinsicObject.getMimeType()));
			iheXdsInfo.setMimeType(ebXMLExtrinsicObject.getMimeType());			
			iheXdsInfoList.add(iheXdsInfo);
			dto.setIheXdsInfoList(iheXdsInfoList);
			dataDao.insertFileData(dto);				
			
			savePatientInfoAndFileMoreInfo(ebXMLExtrinsicObject);
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
	
		return flag;
	}

	public boolean documentREPLACE(EbXMLAssociation ebXMLAssociation, EbXMLSubmitObjectsRequest30 ebXMLBody) {

		boolean flag = true;
		try {
			EbXMLRegistryPackage ebXMLRegistryPackage = Iti41Repository_backup.getEbXMLRegistryPackageByID(ebXMLAssociation.getSource(), ebXMLBody.getRegistryPackages());
			// 存储注册信息
			XdsDto dto = new XdsDto();
			dto.setAssociationType(AssociationType.REPLACE);

			List<IheXdsFileInfo> iheXdsInfoList = new ArrayList<IheXdsFileInfo>();
			IheXdsFileInfo iheXdsInfo = new IheXdsFileInfo();
			String uniqueId=ebXMLRegistryPackage.getExternalIdentifierValue(SysConsts.XDSSubmissionSetuniqueIdidentificationScheme);
			iheXdsInfo.setUniqueId(uniqueId);			
			iheXdsInfoList.add(iheXdsInfo);
			dto.setIheXdsInfoList(iheXdsInfoList);
			
			DataDao dataDao = new DataDao();
			dataDao.updateFileData(dto);
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
	
		return flag;
	}

	public boolean documentAPPEND(EbXMLAssociation ebXMLAssociation, EbXMLSubmitObjectsRequest30 ebXMLBody) {

		boolean flag = true;
		try {
			EbXMLRegistryPackage ebXMLRegistryPackage = Iti41Repository_backup.getEbXMLRegistryPackageByID(ebXMLAssociation.getSource(), ebXMLBody.getRegistryPackages());
			// 存储注册信息
			XdsDto dto = new XdsDto();
			dto.setAssociationType(AssociationType.APPEND);
			
			List<IheXdsFileInfo> iheXdsInfoList = new ArrayList<IheXdsFileInfo>();
			IheXdsFileInfo iheXdsInfo = new IheXdsFileInfo();
			String uniqueId=ebXMLRegistryPackage.getExternalIdentifierValue(SysConsts.XDSSubmissionSetuniqueIdidentificationScheme);
			iheXdsInfo.setUniqueId(uniqueId);			
			iheXdsInfoList.add(iheXdsInfo);
			dto.setIheXdsInfoList(iheXdsInfoList);
			
			DataDao dataDao = new DataDao();
			dataDao.updateFileData(dto);
			
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		return flag;
	}

	public boolean documentTRANSFORM(EbXMLAssociation ebXMLAssociation, EbXMLSubmitObjectsRequest30 ebXMLBody) {

		boolean flag = true;
		try {
			EbXMLExtrinsicObject ebXMLExtrinsicObject = Iti41Repository_backup.getFileMineTypeByDocumentID(ebXMLAssociation.getTarget(), ebXMLBody.getExtrinsicObjects());
			EbXMLRegistryPackage ebXMLRegistryPackage = Iti41Repository_backup.getEbXMLRegistryPackageByID(ebXMLAssociation.getSource(), ebXMLBody.getRegistryPackages());
			// 存储注册信息
			XdsDto dto = new XdsDto();
			dto.setAssociationType(AssociationType.TRANSFORM);
			List<IheXdsFileInfo> iheXdsInfoList = new ArrayList<IheXdsFileInfo>();
			IheXdsFileInfo iheXdsInfo = new IheXdsFileInfo();
			String uniqueId=ebXMLRegistryPackage.getExternalIdentifierValue(SysConsts.XDSSubmissionSetuniqueIdidentificationScheme);
			iheXdsInfo.setUniqueId(uniqueId);			
//			iheXdsInfo.setFilePath(SysConsts.fileRepositoryPath+uniqueId+DhccFileUtils.getFileType(ebXMLExtrinsicObject.getMimeType()));
			iheXdsInfo.setMimeType(ebXMLExtrinsicObject.getMimeType());			
			iheXdsInfoList.add(iheXdsInfo);
			dto.setIheXdsInfoList(iheXdsInfoList);
			DataDao dataDao = new DataDao();
			dataDao.updateFileData(dto);
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}
		return flag;
	}

	public void savePatientInfoAndFileMoreInfo(EbXMLExtrinsicObject ebXMLExtrinsicObject){
		try {
			List<String> list=ebXMLExtrinsicObject.getSlotValues("sourcePatientInfo");		
			for(String s:list) {
				log.info(s);
			}
		} catch (Exception e) {
			log.info(e.getMessage(), e);
		}		
	}
}
